package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author hu
 * @date 2025/1/29
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

//    @GetMapping
//    public Result listAllEmp(@RequestParam(defaultValue = "1") Integer page,
//                             @RequestParam(defaultValue = "10") Integer pageSize, String name, Integer gender,
//                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//
//        log.info("分页查询参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
//        PageResult<Emp> empPageResult = empService.listAllEmp(page, pageSize, name, gender, begin, end);
//        return Result.success(empPageResult);
//    }

    @GetMapping
    public Result listAllEmp(EmpQueryParam empQueryParam) {

        log.info("分页查询参数：{}",empQueryParam);
        PageResult<Emp> empPageResult = empService.listAllEmp(empQueryParam);
        return Result.success(empPageResult);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工参数：{}",emp);
        empService.save(emp);
        return Result.success();
    }

//    @DeleteMapping
//    public Result deleteEmp(@RequestParam List<Integer> ids){  //复杂的 用集合的话注解不能省略，数组可以Integer[] ids
//        log.info("删除员工id：{}", ids);
//        return Result.success();
//    }

    @DeleteMapping
    public Result deleteEmp(Integer[] ids){
        log.info("删除员工id：{}", Arrays.toString(ids));
        empService.deleteEmp(ids);
        return Result.success();
    }


    /**
     * 根据ID查询
     */

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据ID查询：{}",id);
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("修改员工：{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        List<Emp> lists = empService.list();
        return Result.success(lists);
    }
}
